#include<bits/stdc++.h>
#define ll long long
using namespace std;
int siz[400005],fa[400005],n,a[200005],b[200005],ans;
bool ok[400005];
int find(int u){return (fa[u]?fa[u]=find(fa[u]):u);}
int getans(int u){
	u=find(u);
	int re=max(0,ok[u]+siz[u]-1);
	ok[u]=siz[u]=0;
	return re;
}
int main(){
	ios::sync_with_stdio(0);
  	freopen("pairint.in","r",stdin);
  	freopen("pairint.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n*2;i++)siz[i]=1;
	for(int i=1;i<=n;i++){
		cin>>a[i]>>b[i];
		a[i]=find(a[i]);
		b[i]=find(b[i]);
		if(a[i]!=b[i])fa[a[i]]=b[i],siz[b[i]]+=siz[a[i]],ok[b[i]]|=ok[a[i]];
		else ok[a[i]]=1;
	}
	for(int i=1;i<=n;i++)ans+=getans(a[i])+getans(b[i]);
	cout<<ans;
}
